package com.web.common.properties;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Controller
@RefreshScope
@ConfigurationProperties(prefix = "ignore.web")
public class ServiceAuthenticationEntity {
	
	/** 不进行校验的请求地址 */
	private List<String> ignoreWebUrl = new ArrayList<>();
	
	/** 设置文档分享地址 */
	private List<String> shareDocUrl = new ArrayList<>();
	
	/** 设置文件分享地址 */
	private List<String> shareFileUrl = new ArrayList<>();
	
	/** 登录的首页地址  */
	@Deprecated
	@Value("${web.loginPage:''}")
	private String loginPage = "";
	
	@Deprecated
	@Value("${web.isUseCsrf:false}")
	private boolean isUseCsrf = true;
	
	/** 是否允许跨域访问，true是启用跨域访问校验，false关闭跨域访问校验  */
	@Deprecated
	@Value("${web.isUseCors:false}")
	private boolean isUseCors = true;
	
	/** 设置的cors */
	@Value("${security.cors.origins:''}")
	private String corsOrigins;
	
	/**  白名单IP地址  */
	@Value("${white.list.ip:''}")
	private String[] whiteListIp = {};
	
	private final SecurityLoginEntity securityLoginEntity;
	
	public ServiceAuthenticationEntity(SecurityLoginEntity securityLoginEntity) {
		this.securityLoginEntity = securityLoginEntity;
	}
	
	public void setUrl(List<String> url) {
		for(String item: url) {
			ignoreWebUrl.addAll(Arrays.asList(item.split(",")));
		}
		ignoreWebUrl.add(securityLoginEntity.getProcessingUrl());
	}
	
	public void setShareDocUrl(List<String> shareDocUrl) {
		for(String item: shareDocUrl) {
			List<String> tempList = Arrays.asList(item.split(","));
			for(String str: tempList) {
				if(!ignoreWebUrl.contains(str)) {
					ignoreWebUrl.add(str);
				}
				if(!this.shareDocUrl.contains(str)) {
					this.shareDocUrl.add(str);
				}
			}
		}
	}
	
	public void setShareFileUrl(List<String> shareFileUrl) {
		for(String item: shareFileUrl) {
			List<String> tempList = Arrays.asList(item.split(","));
			for(String str: tempList) {
				if(!ignoreWebUrl.contains(str)) {
					ignoreWebUrl.add(str);
				}
				if(!this.shareFileUrl.contains(str)) {
					this.shareFileUrl.add(str);
				}
			}
		}
	}
}
